package com.ooqiu.gaming;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.Utils;
import com.meiyou.skinlib.AndroidSkin;
import com.miguelbcr.ui.rx_paparazzo2.RxPaparazzo;
import com.ooqiu.gaming.injector.components.AppComponent;
import com.ooqiu.gaming.injector.components.DaggerAppComponent;
import com.ooqiu.gaming.injector.modules.AppModule;
import com.ooqiu.gaming.service.PreLoadX5Service;
import com.ooqiu.gaming.view.ToastUtil;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.tencent.bugly.Bugly;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;

/**
 * @author Eenie
 * @date 2017/11/15
 */

public class GamingApp extends Application {

    private AppComponent mAppComponent;


    @Override
    public void onCreate() {
        super.onCreate();
//        ARouter.openDebug();
//        ARouter.openLog();

//        ARouter.setLogger(new RouteLoger());
        inject();
        initLogUtil();
//        initChannel();
        initX5();
        ToastUtil.init(this);
        initShare();
        RxPaparazzo.register(this);
        Utils.init(this);
        Bugly.init(getApplicationContext(), "26d370d37b", true);
        AndroidSkin.getInstance().init(this);
        initAnalytics();

        ARouter.init(this);
    }

    private void initAnalytics() {
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "");
        MobclickAgent.setSecret(this, "");
        MobclickAgent.openActivityDurationTrack(false);
        UMConfigure.setLogEnabled(true);

    }

    private void inject() {
        mAppComponent = DaggerAppComponent
                .builder()
                .appModule(new AppModule(this))
                .build();
        mAppComponent.inject(this);
    }


    public AppComponent getApplicationComponent() {
        return mAppComponent;
    }


    private void initLogUtil() {
        Logger.addLogAdapter(new AndroidLogAdapter());
    }


    private void initChannel() {
//        ChannelDao channelDao = getApplicationComponent().getDaoSession().getChannelDao();
//        if (channelDao.loadAll().isEmpty()) {
//            List<Channel> channels = new ArrayList<>();
//            channels.add(new Channel(null  , "1", "推荐"  , Channel.MODULE_ID_INDEX, true));
//            channels.add(new Channel(null  , "2", "热门游戏", Channel.MODULE_ID_INDEX, true));
//            channels.add(new Channel(null  , "3", "二次元" , Channel.MODULE_ID_INDEX, true));
//            channels.add(new Channel(null  , "4", "绝地求生", Channel.MODULE_ID_INDEX, true));
//            channels.add(new Channel(null  , "5", "王者荣耀", Channel.MODULE_ID_INDEX, true));
//            channelDao.insertInTx(channels);
//        }
    }


    private void initX5() {
        Intent intent = new Intent(this, PreLoadX5Service.class);
        startService(intent);
    }


    private void initShare() {
        Config.DEBUG = true;
        PlatformConfig.setWeixin("wx0e954a79ea059628", "357590c87bf45a580f8f0eabdd20762d");
        PlatformConfig.setQQZone("1106491373", "DYhDGmkCHowgvgnV");
        PlatformConfig.setSinaWeibo("1273173378", "fa975bd5e4b99db6a6c0243e2e25b95d", "http://sns.whalecloud.com");

        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(this).setShareConfig(config);


    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }
}
